class SessionsController < ApplicationController

  def new
    @title = "Authentification"
  end

  def create
    @user = User.authenticate(params[:session][:email],
                             params[:session][:password])
    if @user.nil?
      flash[:error] = "Combinaison email/password invalide."
      @titre = "S'authentifier'"
      render 'new'
    else
      @template.sign_in(@user)
      redirect_back_or @user.compte
    end
  end

  def destroy
    @template.sign_out
    redirect_to root_path
  end

  private

  def redirect_back_or(default)
    redirect_to(session[:return_to] || default)
    @template.clear_return_to
  end

end
